package resources;

import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.BadLocationException;

import src.Window;

/**
 * classe responsavel por escrever onde o cursor da jtextarea se encontra
 * @author eklauberg
 *
 */
public class CaractPosition implements CaretListener {
	int linha;
	int coluna;
	Window window;
	
	public CaractPosition(Window window) {
		this.window = window;
	}
	
	public int getLinha() {
		return linha;
	}
	public void setLinha(int linha) {
		this.linha = linha;
	}
	public int getColuna() {
		return coluna;
	}
	public void setColuna(int coluna) {
		this.coluna = coluna;
	}
	
	@Override
	public String toString() {
		return " Linha: " + linha + " | Coluna: " + coluna;
	}
	@Override
	public void caretUpdate(CaretEvent e) {
		try {
			int caretpos = window.getTxtCodigo().getCaretPosition();
			//coluna
			int linenum = window.getTxtCodigo().getLineOfOffset(caretpos);
			this.coluna = caretpos - window.getTxtCodigo().getLineStartOffset(linenum)+1;
			//linha
			this.linha = linenum+1;
			window.getLblCaractPosition().setText(this.toString());
		} catch (BadLocationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
 }

